#include <pybind11/pybind11.h>
// Python 的列表(list)无法自动转换为 C++ 的 std::vector<double>，因为你可能忘记包含 pybind11/stl.h 头文件，该文件负责 Python 和 C++ 之间的 STL 容器转换。
#include <pybind11/stl.h> 
#include <string>
#include <vector>

namespace py = pybind11;

class Calculator {
public:
    Calculator() = default;
    
    double add(double a, double b) { return a + b; }
    double subtract(double a, double b) { return a - b; }
    
    void set_name(const std::string &name) { this->name = name; }
    std::string get_name() const { return name; }
    
private:
    std::string name;
};

// 处理向量
std::vector<double> process_vector(const std::vector<double> &input) {
    std::vector<double> result;
    for (auto x : input) {
        result.push_back(x * x);
    }
    return result;
}

PYBIND11_MODULE(example_pybind, m) {
    m.doc() = "pybind11 example plugin";
    
    // 导出函数
    m.def("process_vector", &process_vector, "Process a vector of doubles");
    
    // 导出类
    py::class_<Calculator>(m, "Calculator")
        .def(py::init<>())
        .def("add", &Calculator::add)
        .def("subtract", &Calculator::subtract)
        .def("set_name", &Calculator::set_name)
        .def("get_name", &Calculator::get_name);
}